<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class alat_model extends Model {

    protected $_table = 'Alat';
    protected $_prefix = 'alat';
    
    public function __construct() {
        // Call the Model constructor
        parent::__construct ();
    }

    public function add($data) {
        if (! $data || ! is_array ( $data ))
            return false;
        return $this->db->insert ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    
    }
    public function update($id, $data) {
        if (! $id || ! $this->get ( $id ) || ! $data)
            return array ();

        $this->db->where ( $this->_prefix . 'Id', $id );
        $this->db->limit ( 1 );
        return $this->db->update ( $this->_table, $data ) && ($this->db->affected_rows () > 0) ? true : false;
    }
	public function addStock($id) {
        if (! $id || ! $this->get ( $id ) )
            return array ();

        return $this->db->query ( "UPDATE Alat SET alatStock = alatStock+1 WHERE alatId = '".$id."' " ) && ($this->db->affected_rows () > 0) ? true : false;
    }
	public function delStock($id) {
        if (! $id || ! $this->get ( $id ))
            return array ();
		
        return $this->db->query ( "UPDATE Alat SET alatStock = alatStock-1 WHERE alatId = '".$id."' " ) && ($this->db->affected_rows () > 0) ? true : false;
    }
    
	public function delete($id) {
        if (! $id || ! $this->get ( $id ))
            return array ();
        $this->db->where ( $this->_prefix . 'Id', $id );
        $this->db->limit ( 1 );
        return $this->db->delete ( $this->_table ) && ($this->db->affected_rows () > 0) ? true : false;
    }
    
    public function get($id) {
        if (! $id) {
            return array ();
        }
        $query = $this->db->get_where ( $this->_table, array ($this->_prefix . 'Id' => $id ) );
        return $query->row_array ();
    }
    
    public function getList($limit = 100, $offset = 0, $where=array()) {
        $this->db->from ( $this->_table );
        
        if( isset($where['stock']) )
        	$this->db->where('alatStock > 0');
        
        $this->db->limit ( $limit, $offset );
        $query = $this->db->get ();
        return $query->result_array ();
    }
    public function getCount($where=array()) {
        $this->db->select ( 'count(' . $this->_prefix . 'Id) as count' );
        $this->db->from ( $this->_table );
        
        if( isset($where['stock']) )
        	$this->db->where('alatStock > 0');
        
        $query = $this->db->get ();
        if ($query && ($row = $query->row_array ())) {
            return $row ['count'];
        }
        return 0;
    }
    
    //RM Alat
	public function getListRM( $where=array() ) {
		$this->db->select ( 'RMAlat.*' );
		$this->db->select ( 'Alat.alatNama, Alat.alatTanggal' );
        $this->db->from ( 'RMAlat' );
        $this->db->join ( 'Alat', 'RMAlat.alatId = Alat.alatId' );
        
        if( isset($where['rmAlatId']) )
        	$this->db->where ( 'RMAlat.rmAlatId', $where['rmAlatId'] );
        if( isset($where['rmId']) )
        	$this->db->where ( 'RMAlat.rmId', $where['rmId'] );
        if( isset($where['alatId']) )
        	$this->db->where ( 'RMAlat.alatId', $where['alatId'] );	
        
        $query = $this->db->get ();
        return $query->result_array ();
    }
    
	public function getBiaya( $where=array() ) {
		$this->db->select ( 'SUM(rmAlatHargaJual) AS biaya' );
        $this->db->from ( 'RMAlat' );
        
        if( isset($where['rmId']) )
        	$this->db->where ( 'rmId', $where['rmId'] );
        if( isset($where['alatId']) )
        	$this->db->where ( 'alatId', $where['alatId'] );	
        
        $query = $this->db->get ();
        $result = $query->row_array ();
        
        $biaya = isset($result['biaya']) ? $result['biaya'] : 0;
        return $biaya;
    }
    
	public function addRM($data) {
        if (! $data || ! is_array ( $data ))
            return false;
        return $this->db->insert ( 'RMAlat', $data ) && ($this->db->affected_rows () > 0) ? true : false;
    
    }
    
	public function deleteRM( $where=array() ) {
        
		if( isset($where['rmAlatId']) )
        	$this->db->where ( 'rmAlatId', $where['rmAlatId'] );
		if( isset($where['rmId']) )
        	$this->db->where ( 'rmId', $where['rmId'] );
        if( isset($where['alatId']) )
        	$this->db->where ( 'alatId', $where['alatId'] );
        	
        $this->db->limit ( 1 );
        return $this->db->delete ( 'RMAlat' ) && ($this->db->affected_rows () > 0) ? true : false;
    }
}